$(init);

function init() {

    if (/(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)) {
        $('ons-card.info-and').css('display', 'none');
    } else if (/(Android)/i.test(navigator.userAgent)) {
        $('ons-card.info-ios').css('display', 'none');
    }

    copy();
    getReq();

    $('body').on('click', 'button.back-index', backToIndex)
        .on('click', '#reg-btn', doRegister)
        .on('click', '#getcode-btn', doGetCode);
}

function backToIndex() {
    goto('/mobile.html');
}

function doRegister() {
    var ids = ['mobile', 'qq', 'smscode', 'password', 'repassword', 'invitecode'];
    var infoMap = {};
    var success = true;
    var newIds = [];
    ids.forEach(function(id) {
        if ($(`#${id}`).length < 1) return;
        var val = $(`#${id}`).val();
        infoMap[id] = {
            val: val,
            isValid: REGISTER_REGEXS[id] ? REGISTER_REGEXS[id].test(val) : true,
        };
        newIds.push(id);
    });

    // 确认密码
    if (infoMap['repassword'].val !== infoMap['password'].val) {
        infoMap['repassword'].isValid = false;
    }

    // 展示结果
    newIds.forEach(function(id) {
        if (id === 'invitecode') return;
        if (!infoMap[id].val || infoMap[id].val === '' || !infoMap[id].isValid) {
            $(`ons-icon#${id}-error`).css('display','inline-block');
            $(`ons-icon#${id}-success`).css('display','none');
            success = false;
            return;
        } else {
            $(`ons-icon#${id}-success`).css('display','inline-block');
            $(`ons-icon#${id}-error`).css('display','none');
        }
    });

    if (success) {
        var obj = {};
        for (var key in infoMap) {
            if (key == 'repassword') continue;
            obj[key] = infoMap[key].val;
        }
        promise('POST', URL_BUY_REG, JSON.stringify(obj), cbInfo, null);
    }
}

function cbCode(e) {
    $('#reg-btn').attr("disabled", false);
}

function doGetCode() {
    if(!REGISTER_REGEXS.mobile.test($('#mobile').val())) {
        notifyInfo('请输入正确的手机号码！');
        return;
    }
    var obj = {
        'mobilephone': $('#mobile').val()
    };
    $('#getcode-btn').attr("disabled", true);
    promiseNoMask('GET', URL_SMS_SEND, obj, cbCode, null);
    doCounter(CODE_COUNT, 'getcode-btn');
}

function doCounter(count, id) {
    setTimeout(function() {
        $(`#${id}`).text(--count);
        if (count) {
            doCounter(count, id)
        } else {
            $(`#${id}`).text('获取验证码');
            $(`#${id}`).attr("disabled", false);
        }
    }, 1000);
}

function cbInfo(e) {
    notifyInfo(MSG_REGIS_SUCCESS);
    window.history.go(-1);
}

function getReq() {
    // var url = location.search;
    // console.log(url);
    // if(url.indexOf('?') != -1) {
    //     var str = url.substr(1);
    //     strs = str.split('=');
    //     $('ons-input#invite').val(strs[1]);
    // }
    var url = location.search;
    var theRequest = new Object();
    if (url.indexOf("?") != -1) {
        var str = url.substr(1);
        strs = str.split("&");
        for(var i = 0; i < strs.length; i ++) {
            theRequest[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);
        }
    }
    $('ons-input#invite').val(theRequest['invite']);
}

function copy() {
    var clipboardA = new ClipboardJS('.ca', {text: function () { return '2939085757'; }});
    var clipboardB = new ClipboardJS('.cb', {text: function () { return '3173700944'; }});
    var clipboardC = new ClipboardJS('ons-card.info-and', {text: function () { return 'http://c7.gg/amrN6'; }});
    var clipboardD = new ClipboardJS('ons-card.info-ios', {text: function () { return 'https://m.jsz800.com/'; }});

    clipboardA.on('success', function () {notifyToast('复制成功！');});
    clipboardB.on('success', function () {notifyToast('复制成功！');});
    clipboardC.on('success', function () {notifyToast('复制成功！');});
    clipboardD.on('success', function () {notifyToast('复制成功！');});
}